package com.zxy.zxyrpc.loadBalancer;

import com.zxy.zxyrpc.model.ServiceMetaInfo;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 随机负载器
 *
 * @author zxy
 * @since 2025/5/27
 **/
public class RandomLoadBalancer implements LoadBalancer {
    private final Random random = new Random();

    @Override
    public ServiceMetaInfo select(Map<String, Object> reqParams, List<ServiceMetaInfo> serviceMetaInfoList) {
        // 为空则为空，只有一个就无需随机
        if (serviceMetaInfoList.isEmpty())
            return null;
        int size = serviceMetaInfoList.size();
        if (1 == size)
            return serviceMetaInfoList.get(0);
        return serviceMetaInfoList.get(random.nextInt(size));
    }
}
